from django.forms import ModelForm
from django.forms import widgets as wid
from aj import models
from aj.ajgl import models1
from django.core.exceptions import ValidationError
import datetime

# 案件管理
class Anjian_li(ModelForm):
    class Meta:
        # fields = '__all__'
        exclude = ['aj_zhuangtai', 'aj_chufajine', 'aj_heiwodian', 'aj_wfss', 'aj_user', 'aj_time2', 'sb_time',
                   'xg_time','sheng','shi','xian']
        model = models1.Anjian
    def __init__(self, request, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def clean_aj_time1(self):
        aj_time1 = self.cleaned_data.get("aj_time1")
        now=datetime.datetime.now().date()
        if aj_time1>now:
            raise ValidationError("立案时间不能早于今天")
        return aj_time1


class Anjian_jie(ModelForm):
    class Meta:
        exclude=['sb_time','xg_time','aj_user','sheng','shi','xian']
        model = models1.Anjian

    def __init__(self, request, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def clean_aj_juedingshu(self):
        juedingshu = self.cleaned_data.get("aj_juedingshu")
        zhuangtai = self.cleaned_data.get("aj_zhuangtai")
        if not juedingshu and zhuangtai not in ['3', '4']:
            raise ValidationError("这个字段是必填项")
        return juedingshu

    def clean_aj_chufayiju(self):
        chufayiju = self.cleaned_data.get("aj_chufayiju")
        zhuangtai = self.cleaned_data.get("aj_zhuangtai")
        if not chufayiju and zhuangtai not in ['3', '4']:
            raise ValidationError("这个字段是必填项")
        return chufayiju

    def clean_aj_time2(self):
        aj_time2 = self.cleaned_data.get("aj_time2")
        now = datetime.datetime.now().date()
        if not aj_time2:
            raise ValidationError("这个字段是必填项")
        if aj_time2 > now:
            raise ValidationError("结案时间不能早于今天")
        return aj_time2